package  
{
  import away3d.cameras.lenses.PerspectiveLens;
  import away3d.containers.View3D;
  import away3d.core.math.Number3D;
  import away3d.primitives.Plane;
  import away3d.sprites.MovieClipSprite;
  import flash.display.DisplayObjectContainer;
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.KeyboardEvent;
  import flash.geom.Vector3D;
  import flash.net.FileReference;
  import flash.utils.ByteArray;
  import mge.particles.Descriptor;
  import mge.particles.ParticleSystem;
  import mge.serialization.Serializer;
	/**
   * ...
   * @author ivan
   */
  public class MovieClipSpriteParticles extends Sprite
  {
    [Embed(source='../assets/assets.swf', symbol='AwaySignature')]
    private var rAwaySignature:Class;
    
    [Embed(source = '../../particle_editor/resources/particles.png')]
    private var rParticles:Class;
    
    [Embed(source = '../assets/text.xml', mimeType="application/octet-stream")]
    private var rComicHit:Class;
    
    public function MovieClipSpriteParticles() 
    {
      if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
    }
    
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
      
      addChild(view);
      
      plane.width = plane.height = 1000;
      view.scene.addChild(plane);
      
      view.camera.lens = new PerspectiveLens(); // The important step I was missing!
      view.camera.y = 100;
      view.camera.lookAt(new Number3D(0, 1, 0));      
      
      addEventListener(Event.ENTER_FRAME, onEnterFrame);
      
      
      mcs = new MovieClipSprite(new rAwaySignature());
      mcs.rescale = true;
      mcs.scaling = 0.3;
      view.scene.addChild(mcs);
      
      stage.addEventListener(KeyboardEvent.KEY_DOWN, addParticle);      
		}
    
    private var mcs:MovieClipSprite;
    private var ps:ParticleSystem
    private function addParticle(e:Event):void
    {
      ps = new ParticleSystem(new rParticles, (mcs.movieclip as DisplayObjectContainer));
      
      var desc:Descriptor = new Serializer().deserialize(new XML(new rComicHit), Descriptor) as Descriptor;
      
      ps.descriptor = desc;
      ps.fire();
    }
        
    private function onEnterFrame(e:Event):void
    { 
      view.render();
      if (ps)
        ps.update();
    }
    
    
    private var view:View3D = new View3D( { x:250, y:120 } );
    private var plane:Plane = new Plane( {segments:30} );
    
  }

}